- Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLowestCommonAncestorHld.java
135 lines (112 loc) · 3.16 KB
/
LowestCommonAncestorHld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* An algorithm to find the lowest common ancestor (LCA) of two nodes in a tree using heavy light decomposition (HLD).
*
* Time complexity:
* - Preprocessing: O(V + E)
* - Query: O(log V)
*/
packagecodebook.graph;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.io.PrintWriter;
importjava.util.ArrayList;
importjava.util.StringTokenizer;
publicclassLowestCommonAncestorHld {
staticBufferedReaderbr;
staticPrintWriterout;
staticStringTokenizerst;
staticArrayList<ArrayList<Integer>> adj = newArrayList<ArrayList<Integer>>();
staticint[] depth, parent, chain, size, head;
staticintn, q, chainNum;
publicstaticvoidmain (String[] args) throwsIOException {
br = newBufferedReader(newInputStreamReader(System.in));
out = newPrintWriter(newOutputStreamWriter(System.out));
//br = new BufferedReader(new FileReader("in.txt"));
//out = new PrintWriter(new FileWriter("out.txt"));
// number of nodes
n = readInt();
// number of queries
q = readInt();
depth = newint[n];
parent = newint[n];
chain = newint[n];
size = newint[n];
head = newint[n];
for (inti = 0; i < n; i++) {
adj.add(newArrayList<Integer>());
head[i] = -1;
}
for (inti = 0; i < n - 1; i++) {
inta = readInt() - 1;
intb = readInt() - 1;
adj.get(a).add(b);
adj.get(b).add(a);
}
dfs(0, 0, -1);
getHld(0, -1);
for (inti = 0; i < q; i++)
out.println(getLca(readInt() - 1, readInt() - 1) + 1);
out.close();
}
staticintgetLca (inti, intj) {
while (chain[i] != chain[j]) {
if (depth[head[chain[i]]] < depth[head[chain[j]]])
j = parent[head[chain[j]]];
else
i = parent[head[chain[i]]];
}
if (depth[i] < depth[j])
returni;
returnj;
}
staticvoidgetHld (inti, intprev) {
if (head[chainNum] == -1) {
head[chainNum] = i;
}
chain[i] = chainNum;
intmaxIndex = -1;
for (intj : adj.get(i))
if (j != prev && (maxIndex == -1 || size[maxIndex] < size[j]))
maxIndex = j;
if (maxIndex != -1)
getHld(maxIndex, i);
for (intj : adj.get(i))
if (j != prev && j != maxIndex) {
chainNum++;
getHld(j, i);
}
}
staticvoiddfs (inti, intd, intprev) {
depth[i] = d;
parent[i] = prev;
size[i] = 1;
for (intj : adj.get(i)) {
if (j != prev) {
dfs(j, d + 1, i);
size[i] += size[j];
}
}
}
staticStringnext () throwsIOException {
while (st == null || !st.hasMoreTokens())
st = newStringTokenizer(br.readLine().trim());
returnst.nextToken();
}
staticlongreadLong () throwsIOException {
returnLong.parseLong(next());
}
staticintreadInt () throwsIOException {
returnInteger.parseInt(next());
}
staticdoublereadDouble () throwsIOException {
returnDouble.parseDouble(next());
}
staticcharreadCharacter () throwsIOException {
returnnext().charAt(0);
}
staticStringreadLine () throwsIOException {
returnbr.readLine().trim();
}
}